# Contributor: Koni Marti <koni.marti@gmail.com>
# Maintainer: Koni Marti <koni.marti@gmail.com>
pkgname=mailctl
pkgver=0.9.2
pkgrel=0
pkgdesc="Renewal and authorization of OAuth2 credentials"
url="https://github.com/pdobsan/mailctl"
arch="aarch64 x86_64" # limited by ghc
license="BSD-3-Clause"
depends="gnupg"
makedepends="ghc cabal zlib-dev libffi-dev"
subpackages="$pkgname-doc $pkgname-bash-completion $pkgname-zsh-completion $pkgname-fish-completion"
source="mailctl-$pkgver.tar.gz::https://github.com/pdobsan/mailctl/archive/refs/tags/$pkgver.tar.gz
	cabal.project.freeze
	"

export CABAL_DIR="$srcdir/.cabal"

cabal_update() {
	cd $builddir
	cabal v2-update
	(
		cd "$builddir"
		cabal v2-freeze --shadow-installed-packages
		mv cabal.project.freeze "$startdir/"
	)
}

prepare() {
	default_prepare
	ln -sf "$srcdir/cabal.project.freeze" \
		"$builddir/cabal.project.freeze"
}

build() {
	cabal v2-update
	cabal v2-build mailctl:exes \
		--jobs=${JOBS:-1} \
		--prefix=/usr \
		--docdir=/usr/share/doc/$pkgname \
		--sysconfdir=/etc
}

check() {
	cabal test
}

package() {
	mkdir -p "$pkgdir"

	# See https://github.com/haskell/cabal/issues/6919#issuecomment-761563498
	cabal list-bin mailctl:exes | xargs install -Dm755 -t "$pkgdir"/usr/bin
	install -Dm644 README.md -t "$pkgdir"/usr/share/doc/$pkgname/
	install -Dm644 configs/* -t "$pkgdir"/usr/share/$pkgname

	cd completions
	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
d179eca8a24c819cab7e3d7337f12b5321d69baba2e0f61613c1ad8a438261ffe54ad78b77407576c67216f83fbfcd142e5fffa918181165e326b748ffe9d4f4  mailctl-0.9.2.tar.gz
387f833c20e9055340e1ef71a997d741e5e5acf44e47ef7ed7305fb8787b210db98f2526a972cba99c1e1b1dc0eaf2527e03e4d7fe0d7c17c3d62cc65d78403c  cabal.project.freeze
"
